import { loginAPI, userInfoAPI } from '@/api'
import { getToken, removeToken, setToken } from '@/utils/auth'

export default {
  state: {
    token: getToken(),
    userInfo: {}
  },
  mutations: {
    setToken(state, perload) {
      state.token = perload
      setToken(perload)
    },
    removeToken(state) {
      state.token = null
      removeToken()
    },
    setUserInfo(state, preload) {
      state.userInfo = preload
    }
  },
  actions: {
    async login(store, data) {
      store.commit('setToken', await loginAPI(data))
    },
    async getUserInfo(store) {
      store.commit('setUserInfo', await userInfoAPI())
    },
    logout(store) {
      store.commit('removeToken')
      store.commit('setUserInfo', {})
    }
  },
  namespaced: true

}
